package com.calsoft.Hops_Mobile_Tablet;
/*
 * User: Minh Diep
 * Date: 6/26/13
 * Time: 12:36 PM
 */


import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.calsoft.Hops_Mobile_Tablet.base.App;
import com.calsoft.Hops_Mobile_Tablet.base.OpToursItemAdapter;
import com.calsoft.Hops_Mobile_Tablet.base.OpToursItemData;
import com.calsoft.Hops_Mobile_Tablet.component.CategoryChoiceDialogFragment;
import com.calsoft.Hops_Mobile_Tablet.dummy.StaticData;
import com.calsoft.Hops_Mobile_Tablet.sql.OpToursDbAdapter;
import com.calsoft.Hops_Mobile_Tablet.util.SharedPrefs;

public class OpToursFragment extends Fragment implements CategoryChoiceDialogFragment.ChoiceListener {


    private OpToursDbAdapter mDbHelper;
    private OpToursItemAdapter opToursItemAdapter;
    private DialogFragment categoryChoicePicker;
    private boolean[] mSelectedItems;
    private SharedPrefs sPrefs;
    private int currentPage;
    private ListView listView;

    private String itemId = "";
    private String dateCard = "";
    private int startTimeCard;
    private int endTimeCard;
    private String currentLocation = "";
    public static final String OP_TOURS_LAND_SERVICE_CODE = "com.calsoft.hops.mobile.op.tours.fragment.land.service.code";
    public static final String OP_TOURS_DATE = "com.calsoft.hops.mobile.op.tours.fragment.date";
    public static final String OP_TOURS_START_TIME = "com.calsoft.hops.mobile.op.tours.fragment.start.time";
    public static final String OP_TOURS_END_TIME = "com.calsoft.hops.mobile.op.tours.fragment.end.time";

    public OpToursFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_op_tours, container, false);
        getActivity().setTitle("OP Tours");

        listView = (ListView) rootView.findViewById(R.id.service_list);


        StaticData staticData = new StaticData();

        sPrefs = new SharedPrefs(getActivity().getApplicationContext());
        currentPage = sPrefs.getInt(SharedPrefs.CURRENT_PAGE, 0);
        if (currentPage == App.APP_MENU_HOME) {
            Bundle bundle = getArguments();
            itemId = bundle.getString(CardUIDisplayActivity.CARD_UI_ITEM_ID, "");
            dateCard = bundle.getString(CardUIDisplayActivity.CARD_UI_DATE, "");
            startTimeCard = bundle.getInt(CardUIDisplayActivity.CARD_UI_START_TIME, 0);
            endTimeCard = bundle.getInt(CardUIDisplayActivity.CARD_UI_END_TIME, 0);
            currentLocation = staticData.locationHashMap.get(dateCard);
        } else if (currentPage == App.APP_MENU_OP_TOURS) {
            currentLocation = sPrefs.getString(SharedPrefs.CURRENT_LOCATION, "");
            if (currentLocation.equals("CAIRNS")) {
                currentLocation = "CNS";
            } else if (currentLocation.equals("GOLD COAST")) {
                currentLocation = "OOL";
            } else {
                currentLocation = "";
            }
        }

        //Open our database
        mDbHelper = new OpToursDbAdapter(getActivity());


        return rootView;
    }

    private void setupData() {
        opToursItemAdapter = null;
        opToursItemAdapter = new OpToursItemAdapter(getActivity().getApplicationContext());
        Cursor cursor = mDbHelper.fetchAllServiceByLocation(currentLocation, mSelectedItems);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            OpToursItemData item = new OpToursItemData(cursor);
            opToursItemAdapter.addItem(item);
            cursor.moveToNext();
        }
        cursor.close();
    }

    @Override
    public void onResume() {
        super.onResume();
        mDbHelper.open();
        // Empty constructor required for fragment subclasses
        mSelectedItems = new boolean[App.TOURS_CATEGORIES.length];
        for (int i = 0; i < mSelectedItems.length; i++) {
            mSelectedItems[i] = sPrefs.getBoolean(SharedPrefs.CATEGORY_FILTER + App.TOURS_CATEGORIES[i], true);
        }

        setupData();
        displayOpToursListView();
    }

    private void displayOpToursListView() {
        listView.setAdapter(opToursItemAdapter);
        listView.setDivider(null);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                OpToursItemData itemClick = opToursItemAdapter.getItem(position);
                // Get the CITY name from this row in the database.
                String landServiceCode = itemClick.getLandServiceCode();
                if (currentPage == App.APP_MENU_HOME)
                    startServiceOverViewActivity(landServiceCode, dateCard, startTimeCard, endTimeCard);
                else if (currentPage == App.APP_MENU_OP_TOURS)
                    startServiceOverViewActivity(landServiceCode);
            }
        });
    }


    private void startServiceOverViewActivity(String landServiceCode, String dateCard, int startTimeCard, int endTimeCard) {
        Intent intent = new Intent(getActivity(), ServiceOverViewActivity.class);
        intent.putExtra(OP_TOURS_LAND_SERVICE_CODE, landServiceCode);
        intent.putExtra(OP_TOURS_DATE, dateCard);
        intent.putExtra(OP_TOURS_START_TIME, startTimeCard);
        intent.putExtra(OP_TOURS_END_TIME, endTimeCard);
        startActivity(intent);
    }

    private void startServiceOverViewActivity(String landServiceCode) {
        Intent intent = new Intent(getActivity(), ServiceOverViewActivity.class);
        intent.putExtra(OP_TOURS_LAND_SERVICE_CODE, landServiceCode);
        startActivity(intent);
    }


    @Override
    public void onStop() {
        super.onStop();
        mDbHelper.close();

    }

    @Override
    public void onDialogPositiveClick(DialogFragment dialog, boolean[] selectedItems) {
        dialog.dismiss();
        for (int i = 0; i < mSelectedItems.length; i++) {
            mSelectedItems[i] = selectedItems[i];
            sPrefs.onlyPutBoolean(SharedPrefs.CATEGORY_FILTER + App.TOURS_CATEGORIES[i], selectedItems[i]);
        }
        sPrefs.commit();
        setupData();
        displayOpToursListView();
    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        dialog.dismiss();
    }

    public void showCategoryFilterDialog() {
        categoryChoicePicker = new CategoryChoiceDialogFragment(OpToursFragment.this, mSelectedItems);
        categoryChoicePicker.show(getActivity().getSupportFragmentManager(), "CategoryFragment");
    }
}
